// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 



using System;
using System.Collections;

namespace SoapTrace.Core
{
	/// <summary>
	///     <para>
	///       A collection that stores <see cref='SoapPacketClient'/> objects.
	///    </para>
	/// </summary>
	/// <seealso cref='SoapPacketClientCollection'/>
	[Serializable()]
	public class SoapPacketClientCollection : CollectionBase {
		
		/// <summary>
		///     <para>
		///       Initializes a new instance of <see cref='SoapPacketClientCollection'/>.
		///    </para>
		/// </summary>
		public SoapPacketClientCollection()
		{
		}
		
		/// <summary>
		/// Finds a soap packet client with the specified address.
		/// </summary>
		/// <param name="localEndPoint">The client's IP address and port 
		/// (i.e. "1.2.3.4:5555").</param>
		/// <returns>A soap packet client with that matches
		/// the specified end point string.</returns>
		public SoapPacketClient Find( string localEndPoint )
		{
			SoapPacketClient MatchedClient = null;
			
			for( int i = 0; i < InnerList.Count; ++i )
			{
				SoapPacketClient Client = ( SoapPacketClient )( InnerList[ i ] );
				
				if( Client.LocalEndPoint == localEndPoint )
				{
					MatchedClient = Client;
					break;
				}
			}
			
			return MatchedClient;
		}		
		
		/// <summary>
		///     <para>
		///       Initializes a new instance of <see cref='SoapPacketClientCollection'/> based on another <see cref='SoapPacketClientCollection'/>.
		///    </para>
		/// </summary>
		/// <param name='val'>
		///       A <see cref='SoapPacketClientCollection'/> from which the contents are copied
		/// </param>
		public SoapPacketClientCollection(SoapPacketClientCollection val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		///     <para>
		///       Initializes a new instance of <see cref='SoapPacketClientCollection'/> containing any array of <see cref='SoapPacketClient'/> objects.
		///    </para>
		/// </summary>
		/// <param name='val'>
		///       A array of <see cref='SoapPacketClient'/> objects with which to intialize the collection
		/// </param>
		public SoapPacketClientCollection(SoapPacketClient[] val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='SoapPacketClient'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		///    <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public SoapPacketClient this[int index] {
			get {
				return ((SoapPacketClient)(List[index]));
			}
			set {
				List[index] = value;
			}
		}
		
		/// <summary>
		///    <para>Adds a <see cref='SoapPacketClient'/> with the specified value to the 
		///    <see cref='SoapPacketClientCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='SoapPacketClient'/> to add.</param>
		/// <returns>
		///    <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='SoapPacketClientCollection.AddRange'/>
		public int Add(SoapPacketClient val)
		{
			return List.Add(val);
		}
		
		/// <summary>
		/// <para>Copies the elements of an array to the end of the <see cref='SoapPacketClientCollection'/>.</para>
		/// </summary>
		/// <param name='val'>
		///    An array of type <see cref='SoapPacketClient'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		///   <para>None.</para>
		/// </returns>
		/// <seealso cref='SoapPacketClientCollection.Add'/>
		public void AddRange(SoapPacketClient[] val)
		{
			for (int i = 0; i < val.Length; i++) {
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///     <para>
		///       Adds the contents of another <see cref='SoapPacketClientCollection'/> to the end of the collection.
		///    </para>
		/// </summary>
		/// <param name='val'>
		///    A <see cref='SoapPacketClientCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		///   <para>None.</para>
		/// </returns>
		/// <seealso cref='SoapPacketClientCollection.Add'/>
		public void AddRange(SoapPacketClientCollection val)
		{
			for (int i = 0; i < val.Count; i++)
			{
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		///    <see cref='SoapPacketClientCollection'/> contains the specified <see cref='SoapPacketClient'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='SoapPacketClient'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='SoapPacketClient'/> is contained in the collection; 
		///   otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='SoapPacketClientCollection.IndexOf'/>
		public bool Contains(SoapPacketClient val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='SoapPacketClientCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		///    specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='SoapPacketClientCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		///   <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='SoapPacketClientCollection'/> is greater than the available space between <paramref name='arrayIndex'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo(SoapPacketClient[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		///    <para>Returns the index of a <see cref='SoapPacketClient'/> in 
		///       the <see cref='SoapPacketClientCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='SoapPacketClient'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='SoapPacketClient'/> of <paramref name='val'/> in the 
		/// <see cref='SoapPacketClientCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='SoapPacketClientCollection.Contains'/>
		public int IndexOf(SoapPacketClient val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='SoapPacketClient'/> into the <see cref='SoapPacketClientCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='SoapPacketClient'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='SoapPacketClientCollection.Add'/>
		public void Insert(int index, SoapPacketClient val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		///    <para>Returns an enumerator that can iterate through 
		///       the <see cref='SoapPacketClientCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new SoapPacketClientEnumerator GetEnumerator()
		{
			return new SoapPacketClientEnumerator(this);
		}
		
		/// <summary>
		///    <para> Removes a specific <see cref='SoapPacketClient'/> from the 
		///    <see cref='SoapPacketClientCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='SoapPacketClient'/> to remove from the <see cref='SoapPacketClientCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove(SoapPacketClient val)
		{
			List.Remove(val);
		}
		
		/// <summary>
		/// The soap packet client collection enumerator.
		/// </summary>
		public class SoapPacketClientEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			/// <summary>
			/// Creates a new instance of the <see cref="SoapPacketClientEnumerator"/>
			/// class.
			/// </summary>
			/// <param name="mappings">The collection to enumerate.</param>
			public SoapPacketClientEnumerator(SoapPacketClientCollection mappings)
			{
				this.temp = ((IEnumerable)(mappings));
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			/// <summary>
			/// Gets the current item.
			/// </summary>
			public SoapPacketClient Current {
				get {
					return ((SoapPacketClient)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current {
				get {
					return baseEnumerator.Current;
				}
			}
			
			/// <summary>
			/// Moves to the next item in the collection.
			/// </summary>
			/// <returns></returns>
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			/// <summary>
			/// Resets the enumeration.
			/// </summary>
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}
}
